


export var eventBus = new Vue({
    
    data: {
        list: [],
        doneList:[],
    },

    computed: {
        indexlist() {
            return this.list.length
        } , 
        indexDoneList() {
            return this.doneList.length
        },
    },

    created() {        
        // 从本地存储加载
        let todoList = JSON.parse(localStorage.getItem("list"));
        console.log(todoList==true);
        if (todoList) {
            this.list = todoList;
        }
        
        let doneList = JSON.parse(localStorage.getItem("doneList"));
        if (doneList) {
            this.doneList = doneList;
        }
        // 为事件总线添加自定义事件
        this.$on("add",this.add)
        this.$on("finished", this.finished);
        this.$on("del", this.delClick);
        this.$on("del1", this.del1Click);
    },

    watch: {
        list(){
            localStorage.setItem("list", JSON.stringify(this.list));           
        },
        doneList(){         
            localStorage.setItem("doneList", JSON.stringify(this.doneList));     
        }
    },
    

    methods: {
        add(msg) {
            if (this.list.indexOf(msg) < 0) {
                this.list.push(msg)
            } else {
                alert("输入重复")
            }
        },
        finished(index){
            console.log("我是仓库的finished函数");
            let it = this.list.splice(index,1);
            this.doneList.push(it.toString())
        },
        delClick(index) {
            console.log("我是仓库的delClick函数");
            this.list.splice(index,1);
            
        },
        del1Click(index) {
            console.log("我是仓库完成列表的delClick函数");

            this.doneList.splice(index,1);
        }
    },

})